$(document).ready(function() {

    $("#object_edit_update_submit").click(function() {

        $("#update_checkform").hide();
        $(this).hide();
            alert('更新成功');
        $("form").submit();

    });


    //如果為 0 清空	
    $("input").each(function() {
        if ($(this).val() == 0) {
            //$(this).val('');
        }
    });
    
    
    $("#type_rent").click(function(){
    	    		
    		if( $("#type_rent").prop("checked") ){
	    		object_serial();
	    }	    
    });
    
    $("#type_rent").click(function(){  
    	//alert('qq');  	    		
    		if( $("#type_rent").prop("checked") ){
	    		object_serial();
	    }	    
    });
	
	$("#type_sale").click(function(){    	    		
    		if( $("#type_sale").prop("checked") ){
	    		object_serial();
	    }	    
    });
    
    $("#object_usage").change(function(){    	    		
    		//if( $("#type_sale").prop("checked") ){
	    		object_serial();
	    //}	    
    });
    
});



function object_serial(){
		$.post("/object/object_serial", {
		       type_rent: $("#type_rent").prop("checked") , 
		       type_sale:$("#type_sale").prop('checked'), 
		       object_usage:$("#object_usage").val(),
		        
		    }, function(result) {
		        $("#object_no").val(result);
		        $("#market_survey_no").val(result);
		        //market_survey_no
		    });
}

function selectCheckBox() {
    if ($("#select_client").attr("checked")) {
        $("[id='demand_check_box']").attr("checked", 'true');
    } else {
        $("[id='demand_check_box']").removeAttr("checked");
    }
}

function match_form() {

    $("form").attr("action", "/pair/object_pattern");
    $("form").submit();

}

//更新到劉老大那邊的啦
function update_checkform() {
    $("#object_checkform").hide();
    $("#broker").val(1);
    $("#front_man").val(1);
    alert('更新成功！');
    $("form").submit();
}

function checkform() {
    alert('輸入成功！');
    $("#object_checkform").hide();
    $("#reset").hide();
    $("form").submit();

}

function select_rent() {
    if ($("#type_rent").attr("checked") == "checked")
        $("#rent_case").show();
    else
        $("#rent_case").hide();
}

function select_sale() {
    if ($("#type_sale").attr("checked") == "checked")
        $("#sale_case").show();
    else
        $("#sale_case").hide();
}

function get_line(id) {
    $.post("/mrt/get_mrt_ajax/", {
        mrt_line_id: $("#mrt_line" + id).val()
    }, function(result) {
        if (result) {
            $("#mrt" + id).html(result);
        } else {
            alert("出錯");
        }
    });
}

function show_emphasis_area(id) {
    $("input[type=radio][name=emphasis_area_value][value=0]").attr("checked", "checked");
    $('#emphasis_area_select').dialog({
        autoOpen: true,
        modal: true,
        width: 620,
        buttons: {
            "確定": function() {
                $('#' + id).val($("input[name='emphasis_area_value']:checked").val());
                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });

}

function circle_city_choose() {

    $.post("/address/county", {
        id: $("#circle_city").val()
    }, function(result) {
        if (result != "") {
            $("#circle_country").html(result);
        }
    });
}


//商圈
function circle_country_choose() {


    //alert ( $('#circle_city').find("option:selected").text()+
        //"   "+ $('#circle_country').find("option:selected").text()
        //);
    $.post("/circle/circle_josn", {
        city: $('#circle_city').find("option:selected").text(),
        area: $('#circle_country').find("option:selected").text()
    }, function(result) {
        if (result != "") {

            $("#circle").html(result);

        }

    });
}

function circle_choose() {
    $('#circle_value').val($('#circle').find("option:selected").text());
}

function show_circle(id) {

    $('#circle_value').val($('#' + id).val());
    $('#circle_select').dialog({
        autoOpen: true,
        modal: true,
        width: 480,
        buttons: {
            "確定": function() {
                if ($('#circle_value').val() != "")
                    $('#' + id).val($('#circle_value').val());
                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });
}

//step 1

function show_administrative_area(id) {
    $('#administrative_area_select').dialog({
        autoOpen: true,
        modal: true,
        width: 300,
        height: 550,
        buttons: {
            "確定": function() {
                if ($('#administrative_area_value').val() != "") {

                    var h_a = $("#hous_ebuilding_administrative_area").html();

                    $('#' + id).val(
                    	$('#administrative_city').find("option:selected").text()+
                    	$('#administrative_area_value').val());

                    
                    //建物門牌	行政區 1
                    $.post("/address/administrative_area", {
                        id: $("#administrative_country :selected").val()
                    }, function(result) {
                        //alert("WTFFF");
                        if (result != "") {
                            $("#hous_ebuilding_street").html(result);
                            $("#house_building").val("");
                        }
                    });

                    ////////************************************************////////////////////

                    $.post("/circle/circle_josn", {
                        city: $('#administrative_city').find("option:selected").text(),
                        area: $('#administrative_country').find("option:selected").val()
                    }, function(result) {
                        if (result != "") {

                            $("#circle").html(result);

                        }

                    });

                    ////////************************************************////////////////////
                    //商圈1

                    ////////************************************************////////////////////

                }
                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });

}

function administrative_area_choose() {
    /******/
    $('#administrative_area_value').val($('#administrative_country').find("option:selected").text());

}

function administrative_area_city_choose() {
    $.post("/address/county", {
        id: $("#administrative_city").val()
    }, function(result) {
        if (result != "") {
            $("#administrative_country").html(result);
        }
    });
}

function show_copy_object() {

    //alert($('#object_id').val() + ' name: ' + $("#owner").val() );
    //alert($("#fucking_object_id").val() +$("#c_name").val()+$("#c_mobile").val()+$("#c_phone").val());

    $('#copy_object_div').dialog({
        autoOpen: true,
        modal: true,
        width: 400,
        buttons: {
            "確定": function() {

                $.post("/object/copy_object", {

                    object_id: $("#fucking_object_id").val(),
                    name: $("#c_name").val(),
                    mobile: $("#c_mobile").val(),
                    phone: $("#c_phone").val()

                }, function(result) {
                    //alert($("#fucking_object_id").val());
                    alert('複製物件成功');
                    //if (result == "1") {
                    //	alert("複製物件成功!");
                    //}
                });

                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });

}

function show_use_area() {

    $('#use_area_select').dialog({
        autoOpen: true,
        modal: true,
        width: 500,
        buttons: {
            "確定": function() {
                if ($("#use_area").val() == "")
                    $("#use_area").val(0);
                if ($("#courtyard_area").val() == "")
                    $("#courtyard_area").val(0);
                if ($("#basement").val() == "")
                    $("#basement").val(0);
                if ($("#terrace").val() == "")
                    $("#terrace").val(0);
                if ($("#additional").val() == "")
                    $("#additional").val(0);
                $("#use_area").val(0);
                var ua = 0;
                ua = parseInt($("#use_area").val()) + parseInt($("#courtyard_area").val()) + parseInt($("#basement").val()) + parseInt($("#terrace").val()) + parseInt($("#additional").val());
                $("#use_area").val(ua);
                //set_main_building_area(ua);
                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });

}

//附屬建物面積
function subsidiary() {

    $('#subsidiary').dialog({
        autoOpen: true,
        modal: true,
        width: 300,
        buttons: {
            "確定": function() {
                if ($("#sun_area").val() == "")
                    $("#sun_area").val(0);
                if ($("#flower_sun").val() == "")
                    $("#flower_sun").val(0);
                if ($("#falt_sun").val() == "")
                    $("#falt_sun").val(0);
                if ($("#terrace_flat").val() == "")
                    $("#terrace_flat").val(0);
                if ($("#no_sun").val() == "")
                    $("#no_sun").val(0);
                if ($("#subsid_other").val() == "")
                    $("#subsid_other").val(0);

                $("#subsidiary_building_area").val(0);
                var uab = 0;
                uab = parseInt($("#sun_area").val()) + parseInt($("#flower_sun").val()) + parseInt($("#falt_sun").val()) + parseInt($("#terrace_flat").val()) + parseInt($("#no_sun").val()) + parseInt($("#subsid_other").val());

                $("#subsidiary_building_area").val(uab);
                set_main_building_area(uab);
                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });

}

//公設
function pubilc_set() {

    $('#pubilc_set').dialog({
        autoOpen: true,
        modal: true,
        width: 300,
        buttons: {
            "確定": function() {
                if ($("#p_main").val() == "")
                    $("#p_main").val(0);
                if ($("#p_big").val() == "")
                    $("#p_big").val(0);
                if ($("#p_small").val() == "")
                    $("#p_small").val(0);

                $("#postulate").val(0);
                var uua = 0;
                uua = parseInt($("#p_main").val()) + parseInt($("#p_big").val()) + parseInt($("#p_small").val());
                $("#postulate").val(uua);
                set_main_building_area(uua);
                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });

}

//主建物面積
function main_build_area() {

    $('#main_build_area').dialog({
        autoOpen: true,
        modal: true,
        width: 300,
        buttons: {
            "確定": function() {
                if ($("#isfloor").val() == "")
                    $("#isfloor").val(0);
                if ($("#staircases").val() == "")
                    $("#staircases").val(0);
                if ($("#m_b_other").val() == "")
                    $("#m_b_other").val(0);
                if ($("#main_area").val() == "")
                    $("#main_area").val(0);
                //main_build_area_main_area

                $("#main_building_area").val(0);

                var uuaac = 0;
                uuaac = parseInt($("#isfloor").val()) + parseInt($("#staircases").val()) + parseInt($("#main_area").val()) + parseInt($("#m_b_other").val());
                $("#main_building_area").val(uuaac);

                var building_area_val = $("#building_area").val();
                building_area_val = 0;
                building_area_val = parseInt(building_area_val) + parseInt(uuaac);

                //var building_area_val = $("#building_area").val();
                //building_area_val = parseInt(building_area_val) + parseInt(uuaac);

                $("#building_area").val(building_area_val);

                var building_area_val2 = $("#building_area").val();
                var postulate = $("#postulate").val();

                //alert(  building_area_val2 + ' , ' +  postulate);

                if (parseInt(postulate) != '' && parseInt(building_area_val2) != '') {

                    $("#postulate_than").val(parseInt(building_area_val2) / parseInt(postulate));

                }

                set_main_building_area(0);

                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });

}

//室內格局
function inside_crid() {

    $('#inside_crid').dialog({
        autoOpen: true,
        modal: true,
        width: 300,
        buttons: {
            "確定": function() {
                if ($("#troom").val() == "")
                    $("#troom").val(0);
                if ($("#inside_room").val() == "")
                    $("#inside_room").val(0);

                if ($("#livingroom").val() == "")
                    $("#livingroom").val(0);

                if ($("#restroom").val() == "")
                    $("#restroom").val(0);

                if ($("#bookroom").val() == "")
                    $("#bookroom").val(0);
                if ($("#manageroom").val() == "")
                    $("#manageroom").val(0);
                if ($("#closeroom").val() == "")
                    $("#closeroom").val(0);
                if ($("#saveroom").val() == "")
                    $("#saveroom").val(0);
                if ($("#otherroom").val() == "")
                    $("#otherroom").val(0);

                $("#indoor_pattern_house").val(0);
                $("#indoor_pattern_hall").val(0);
                $("#indoor_pattern_guard").val(0);
                $("#indoor_pattern_suite").val(0);

                var indoor_pattern_house = 0;
                var indoor_pattern_hall = 0;
                var indoor_pattern_guard = 0;
                var indoor_pattern_suite = 0;

                indoor_pattern_house = parseInt($("#troom").val()) + parseInt($("#bookroom").val()) + parseInt($("#manageroom").val()) + parseInt($("#closeroom").val()) + parseInt($("#saveroom").val()) + parseInt($("#otherroom").val());

                indoor_pattern_hall = parseInt($("#livingroom").val());

                indoor_pattern_guard = parseInt($("#restroom").val());

                indoor_pattern_suite = parseInt($("#inside_room").val());

                $("#indoor_pattern_house").val(indoor_pattern_house);
                $("#indoor_pattern_hall").val(indoor_pattern_hall);
                $("#indoor_pattern_guard").val(indoor_pattern_guard);
                $("#indoor_pattern_suite").val(indoor_pattern_suite);

                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });

}

function statistics_year_of_construction() {
    var ymd = $("#building_completion_date").val().split('/');
    var d = new Date(ymd[0], ymd[1], ymd[2]);
    var myDate = new Date();
    var interval = parseInt(myDate.getTime() - d.getTime());
    var day = parseInt(interval / (365 * 24 * 60 * 60 * 1000) + 1);
    $("#year_of_construction").val(day);
}

function set_main_building_area(add_value) {

    var building_area_val = $("#building_area").val();
    var main_building_area = $("#main_building_area").val();
    var subsidiary_building_area = $("#subsidiary_building_area").val();
    var postulate = $("#postulate").val();

    if (isNaN(subsidiary_building_area) || subsidiary_building_area == 0 || subsidiary_building_area == '') {
        subsidiary_building_area = 0;
    }

    if (isNaN(main_building_area) || main_building_area == 0 || main_building_area == '') {
        main_building_area = 0;
    }

    if (isNaN(postulate) || postulate == 0 || postulate == '') {
        postulate = 0;
    }
    //postulate
    //subsidiary_building_area
    /*
     alert(
     parseInt(postulate) +","+
     parseInt(building_area_val) +","+
     parseInt(subsidiary_building_area)
     );
     */

    building_area_val = 0;
    building_area_val = parseInt(postulate) + parseInt(main_building_area) + parseInt(subsidiary_building_area);

    $("#building_area").val(building_area_val);

    var building_area_val2 = $("#building_area").val();
    var postulate = $("#postulate").val();

    if (parseInt(postulate) > 0 && parseInt(building_area_val2) > 0) {

        //alert( building_area_val2 +';' + postulate );

        var p_num = 0;
        var p_num2 = 0;
        p_num += Math.floor(parseInt(building_area_val2) / parseInt(postulate));
        p_num2 = division(parseInt(building_area_val2), parseInt(postulate));

        //alert(p_num +'|' + p_num2);

        $("#postulate_than").val(p_num.toFixed(2));

    }

}

function division(num1, num2) {
    var t1 = 0, t2 = 0, r1, r2;
    try {
        t1 = num1.toString().split(".")[1].length
    } catch (e) {
    }
    try {
        t2 = num2.toString().split(".")[1].length
    } catch (e) {
    }

    with (Math) {

        r1 = Number(num1.toString().replace(".", ""));
        r2 = Number(num2.toString().replace(".", ""));
        return (r1 / r2) * pow(10, t2 - t1);

    }
}

/*
 function set_stakeholders_land_area() {
 
 var st_land_ar = $("#stakeholders_land_area").val();
 var st_base_area = $("#base_area").val();
 var st_scope_of_rights1 = $("#scope_of_rights1").val();
 var st_scope_of_rights2 = $("#scope_of_rights2").val();
 
 if (st_scope_of_rights1 != "" && st_scope_of_rights2 != "" && st_base_area != "") {
 
 $("#stakeholders_land_area").val(st_base_area * (st_scope_of_rights1 / st_scope_of_rights2));
 
 }
 
 }
 */

$("document").ready(function() {

    $("#pair_submit").click(function() {

        var object_id = $("#fucking_object_id").val();
        //alert(object_id);
        var url = '/pair/object?id=' + object_id;
        //alert(url);
        document.location.href = url;
    });

    $("#object_do_excel").click(function() {

        var values = $('input:checkbox:checked').map(function() {
            return this.value;
        }).get();

        //alert(values);
        $("form#userinfo").attr("action", "/object/excel");
        $("form#userinfo").submit();

    });

    $(":checkbox").each(function() {
        $(this).click(function() {

            if ($(this).attr('checked')) {

                $.post("/object/save_object_id", {
                    id: $(this).val(),
                    "is_del": 0
                }, function(result) {

                });

            } else {

                $.post("/object/save_object_id", {
                    id: $(this).val(),
                    "is_del": 1
                }, function(result) {

                });
            }

        });
    });

    $("#base_area").change(function() {
        //set_stakeholders_land_area();
    });

    $("#scope_of_rights1").change(function() {
        //set_stakeholders_land_area();
    });

    $("#scope_of_rights2").change(function() {
        //set_stakeholders_land_area();
    });

    $("#total_present_value1").change(function() {

        var d = new Date();
        var year = d.getFullYear();
        //var day = d.getDate();

        $("#total_present_value2").val(year);

    });

}); 